package com.daka.controller;

import com.daka.entity.Group;
import com.daka.entity.GroupRecruit;
import com.daka.entity.GroupRecruitVolunteer;
import com.daka.entity.Volunteer;
import com.daka.enumerate.StatusCode;
import com.daka.service.GroupRecruitService;
import com.daka.service.GroupRecruitVolunteerService;
import com.daka.service.GroupService;
import com.daka.service.VolunteerService;
import com.daka.tools.DKQConstant;
import com.daka.tools.Page;
import com.daka.tools.StrUtils;
import com.daka.tools.excel.ExcelUtils;
import com.daka.vo.request.GroupRecruitVolunteerVo;
import com.daka.vo.response.BaseResponseVo;
import com.daka.vo.response.DataResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yangx
 * Date: 2016/8/5 0005
 * Time: 15:29
 */

@Controller
@RequestMapping("/groupRecruit")
@Slf4j
public class GroupRecruitController {

    @Autowired
    private GroupRecruitService groupRecruitService;
    @Autowired
    private GroupRecruitVolunteerService groupRecruitVolunteerService;
    @Autowired
    private VolunteerService volunteerService;
    @Autowired
    private GroupService groupService;


    @ResponseBody
    @RequestMapping(value = "/create")
    public DataResponseVo<Object> create(GroupRecruit po) {
        try {
            if (po.getGroupId() == null || po.getGroupId() == 0) {
                return new DataResponseVo<Object>(StatusCode.PARAMETER_INVALID.getCode(), StatusCode.PARAMETER_INVALID.getMessage());
            }
            if (StrUtils.isNotEmpty(po.getSkill())) {
                po.setSkill(po.getSkill().replace(",", ";"));
            }
            if (StrUtils.isNotEmpty(po.getJob())) {
                po.setSkill(po.getJob().replace(",", ";"));
            }
            if (StrUtils.isNotEmpty(po.getServiceField())) {
                po.setServiceField(po.getServiceField().replace(",", ";"));
            }
            if (StrUtils.isNotEmpty(po.getQuestion())) {
                po.setQuestion(po.getQuestion().replace(",", ";"));
            }
            po.setDelStatus(0);
            po.setApplys(0);
            if (po.getNeeds() == null) {
                po.setNeeds(0);
            }
            po.setNeeds(0);
            if(po.getVolunteerId()!=null && po.getVolunteerId()!=0){
                Volunteer vo = volunteerService.selectByPrimaryKey(po.getVolunteerId());
                if(StrUtils.isNotEmpty(vo.getRealName())){
                    po.setLinkman(vo.getRealName());
                }
            }
            groupRecruitService.insert(po);
            return new DataResponseVo<Object>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new DataResponseVo<Object>(StatusCode.SYS_ERROR.getCode(), StatusCode.SYS_ERROR.getMessage());
        }
    }


    @ResponseBody
    @RequestMapping(value = "/update")
    public DataResponseVo<Object> update(GroupRecruit po) {
        try {
            if (po.getGroupId() == null || po.getGroupId() == 0) {
                return new DataResponseVo<Object>(StatusCode.PARAMETER_INVALID.getCode(), StatusCode.PARAMETER_INVALID.getMessage());
            }
            if (StrUtils.isNotEmpty(po.getSkill())) {
                po.setSkill(po.getSkill().replace(",", ";"));
            }
            if (StrUtils.isNotEmpty(po.getJob())) {
                po.setSkill(po.getJob().replace(",", ";"));
            }
            if (StrUtils.isNotEmpty(po.getServiceField())) {
                po.setServiceField(po.getServiceField().replace(",", ";"));
            }
            if (StrUtils.isNotEmpty(po.getQuestion())) {
                po.setQuestion(po.getQuestion().replace(",", ";"));
            }
            if(po.getVolunteerId()!=null && po.getVolunteerId()!=0){
                Volunteer vo = volunteerService.selectByPrimaryKey(po.getVolunteerId());
                if(StrUtils.isNotEmpty(vo.getRealName())){
                    po.setLinkman(vo.getRealName());
                }
            }
            groupRecruitService.update(po);
            return new DataResponseVo<Object>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new DataResponseVo<Object>(StatusCode.SYS_ERROR.getCode(), StatusCode.SYS_ERROR.getMessage());
        }
    }

    /**
     * zhaomu lie biao
     * @param groupId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryList")
    public DataResponseVo<Object> queryList(@RequestParam(value = "groupId")String groupId) {
        Map<String,String> map = new HashMap<String, String>();
        map.put("groupId", groupId);
        List<GroupRecruit> list = groupRecruitService.queryByEntity(map);
        return new DataResponseVo<Object>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(),list);
    }

    /**
     * zhaomu xiang qing
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryDetail")
    public DataResponseVo<Object> queryDetail(@RequestParam(value = "id")String id) {
        GroupRecruit groupRecruit = groupRecruitService.selectByPrimaryKey(Long.valueOf(id));
        return new DataResponseVo<Object>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(),groupRecruit);
    }

    /**
     * 志愿者审核列表
     * @param po
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryVolList")
    public DataResponseVo<Object> queryVolList(GroupRecruitVolunteerVo po) {

        Page<Volunteer> list = volunteerService.selectByRecruitPage(po);
        return new DataResponseVo<Object>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(),list);
    }


    /**
     * 审核列表excel
     * @param po
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/excel")
    public DataResponseVo<Object> excel(GroupRecruitVolunteerVo po,HttpServletRequest request, HttpServletResponse response) {
        try {
            List<Volunteer> list = volunteerService.selectByRecruit(po);

            String[] Title = new String[]{"姓名", "手机号", "性别", "服务领域", "职业", "技能", "证件类别", "证件号"};// 表头
            String[] includes = new String[]{"realName", "mobile", "sex", "serviceField", "job", "skill", "cardType", "cardno"};

            String result = ExcelUtils.exportExcel(response, "审核-志愿者列表", Title, list, includes);

            return new DataResponseVo<Object>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(),result);
        }catch (Exception e){
            e.printStackTrace();
            return new DataResponseVo<Object>(StatusCode.SYS_ERROR.getCode(), StatusCode.SYS_ERROR.getMessage());
        }
    }


    /**
     * zhiyuanzhe xiang qing
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryVolDetail")
    public DataResponseVo<Object> queryVolDetail(@RequestParam(value = "id")String id) {

        GroupRecruitVolunteer groupRecruitVolunteer = groupRecruitVolunteerService.selectByPrimaryKey(Long.valueOf(id));

        Volunteer volunteer = volunteerService.selectByPrimaryKey(groupRecruitVolunteer.getVolunteerId());

        Map<String,Object> map = new HashMap<String, Object>();
        map.put("vo" ,volunteer);
        map.put("question",groupRecruitVolunteer);
        return new DataResponseVo<Object>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(),map);
    }


    /**
     * 通过
     */
    @ResponseBody
    @RequestMapping(value = "/approve")
    public DataResponseVo<Object> approve(GroupRecruitVolunteer po) {
        try {
            if (po.getStatus() != 0) {
                return new DataResponseVo<Object>(StatusCode.ERROR_STATUS.getCode(), StatusCode.ERROR_STATUS.getMessage());
            }
            po.setStatus(DKQConstant.APPLY_STATUS_OK);
            int a = groupRecruitVolunteerService.update(po);
            if(a<1){
                return new DataResponseVo<Object>(StatusCode.SYS_ERROR.getCode(), StatusCode.SYS_ERROR.getMessage());
            }
            //zuzhi +1
            GroupRecruit groupRecruit = groupRecruitService.selectByPrimaryKey(po.getGroupRecruitId());
            Group group = groupService.selectByPrimaryKey(groupRecruit.getGroupId());
            group.setVolunteers(group.getVolunteers()+1);
            groupService.update(group);
            return new DataResponseVo<Object>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return  new DataResponseVo<Object>(StatusCode.SYS_ERROR.getCode(), StatusCode.SYS_ERROR.getMessage());
        }
    }


    /**
     * 拒绝
     * @param po
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/refuse")
    public DataResponseVo<Object> refuse(GroupRecruitVolunteer po) {
        try {
            if (po.getStatus() != 0) {
                return new DataResponseVo<Object>(StatusCode.ERROR_STATUS.getCode(), StatusCode.ERROR_STATUS.getMessage());
            }
            po.setStatus(DKQConstant.APPLY_STATUS_REFUST);

            groupRecruitVolunteerService.update(po);
            return new DataResponseVo<Object>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return  new DataResponseVo<Object>(StatusCode.SYS_ERROR.getCode(), StatusCode.SYS_ERROR.getMessage());
        }
    }


}












